home *** CD-ROM | disk | FTP | other *** search
/ PC Graphics Unleashed / PC Graphics Unleashed.iso / ch21 / bezier_p.c next >
C/C++ Source or Header  |  1994-08-08  |  3KB  |  157 lines

  1. ///////////////////////////////
  2. //
  3. // CODE TO DRAW A BEZIER PATCH
  4. // BEZIER_P.C
  5. //
  6. //////////////////////////////
  7.  
  8. #include <dos.h>
  9. #include <graphics.h>
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <conio.h>
  13. #include <math.h>
  14.  
  15.  
  16. typedef struct {
  17.   float x,y;
  18. } Point2D;
  19.  
  20. void InitGraphics(void);
  21. void WorldToDevice(float wx, float wy, int * dx, int * dy);
  22. void DrawAPoint2D(Point2D p);
  23. void DrawALine(Point2D p1, Point2D p2);
  24. float Bernstein(int c, float u);
  25. float GetValue(int i);
  26.  
  27. float  WxLeft, WxRight, WyTop, WyBottom;
  28. int    DyMax, DxMax, DyMin, DxMin;
  29.  
  30.  
  31. void main(void)
  32. {
  33.  int i,j;
  34.  float t,tt, B, step;
  35.  Point2D c, cc, cc_old, c_old, pnt, p[4][4];
  36.  
  37.  InitGraphics();
  38.  
  39.  WxLeft   = -3;
  40.  WxRight  =  3;
  41.  WyTop    =  3;
  42.  WyBottom = -3;
  43.  
  44.  DyMin = 0;
  45.  DxMin = 0;
  46.  DyMax = getmaxy();
  47.  DxMax = getmaxy();
  48.  
  49.  
  50.  setviewport((getmaxx()-getmaxy())/2,0,getmaxx(),getmaxy(),1);
  51.  
  52.  setbkcolor(WHITE);
  53.  cleardevice();
  54.  setcolor(BLUE);
  55.  
  56.  for(j=0;j<4;j++)
  57.    for(i=0;i<4;i++) {
  58.       p[i][j].x = GetValue(i)+random(10)*0.1;
  59.       p[i][j].y = GetValue(j);
  60.       DrawAPoint2D(p[i][j]);
  61.    }
  62.  
  63.  
  64.  step = 20.0;
  65.  for(tt=0; tt<=(int)step; tt++) {
  66.   for(t=0; t<=(int)step; t++) {
  67.     c.x = c.y = 0.0;
  68.     cc.x = cc.y = 0.0;
  69.     for(j=0;j<4;j++) {
  70.       for(i=0;i<4;i++) {
  71.     B = Bernstein(i,t/step)*Bernstein(j,tt/step);
  72.     c.x += p[i][j].x * B;
  73.     c.y += p[i][j].y * B;
  74.     B = Bernstein(i,tt/step)*Bernstein(j,t/step);
  75.     cc.x += p[i][j].x * B;
  76.     cc.y += p[i][j].y * B;
  77.       }
  78.     }
  79.     if(!t) {
  80.        c_old = c;
  81.        cc_old = cc;
  82.     }
  83.     else {
  84.     DrawALine(c_old,c);
  85.     DrawALine(cc_old,cc);
  86.     cc_old = cc;
  87.     }
  88.   }
  89.  }
  90.  getch();
  91.  closegraph();
  92. }
  93.  
  94. /* ================================================================== */
  95.  
  96. float GetValue(int i) {
  97.    switch(i) {
  98.       case 0: return(-1.0);
  99.       case 1: return(-0.3);
  100.       case 2: return(0.3);
  101.       default: return(1.0);
  102.    }
  103. }
  104.  
  105. float Bernstein(int c, float u) {
  106.    switch(c) {
  107.       case 0: return (pow((1.0-u),3));
  108.       case 1: return (3.0 * u * pow((1.0-u),2));
  109.       case 2: return (3.0 * pow(u,2) * (1.0-u));
  110.       default: return (pow(u,3));
  111.    }
  112. }
  113.  
  114. void WorldToDevice(float wx, float wy, int * dx, int * dy) {
  115.     *dx = (WxLeft - wx) * DxMax / (WxLeft - WxRight);
  116.     *dy = (WyTop - wy) * DyMax / (WyTop - WyBottom);
  117. }
  118.  
  119. void DrawAPoint2D(Point2D p) {
  120.   int dx, dy;
  121.   WorldToDevice(p.x, p.y, &dx, &dy);
  122.   rectangle(dx-2,dy-2,dx+2,dy+2);
  123. }
  124.  
  125. void DrawALine(Point2D p1, Point2D p2) {
  126.   int dx1, dy1, dx2, dy2;
  127.   WorldToDevice(p1.x, p1.y, &dx1, &dy1);
  128.   WorldToDevice(p2.x, p2.y, &dx2, &dy2);
  129.   line(dx1,dy1,dx2,dy2);
  130. }
  131.  
  132.  
  133. void InitGraphics(void)
  134. {
  135.  int gdriver = DETECT, gmode, errorcode;
  136.  initgraph(&gdriver, &gmode, "");
  137.  errorcode = graphresult();
  138.  if (errorcode != grOk)  /* an error occurred */
  139.  {
  140.    printf("Graphics error: %s\n", grapherrormsg(errorcode));
  141.    printf("Press any key to halt:");
  142.    getch();
  143.    exit(1); /* terminate with an error code */
  144.  }
  145.  setviewport(0,0,getmaxx(),getmaxy(),1);
  146. }
  147.  
  148.  
  149.  
  150.  
  151.  
  152.  
  153.  
  154.  
  155.  
  156.  
  157.